﻿using System.Windows;

namespace ScrumManagementToolUserControls
{
    /// <summary>
    /// Interaction logic for Login.xaml
    /// </summary>
    public partial class LoginRegistration
    {
        public LoginRegistration()
        {
            InitializeComponent();
        }

        #region login screen

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            ScrumManagementTool.login.Login login = new ScrumManagementTool.login.Login();
            bool isLoginSuccessful = login.AttemptLogin(txtLoginUsername.Text, pwdLoginPassword.Password);
            if (isLoginSuccessful)
            {
                grdLogin.Visibility = Visibility.Collapsed;
                MainWindow mainWindow = new MainWindow();
                mainWindow.Show();
                GetTopParent().Close();
            }
            else
                MessageBox.Show("Login unsuccessful!!!");
        }

        private void btnRegistration_Click(object sender, RoutedEventArgs e)
        {
            SwitchToRegistrationScreen();
        }

        private void pwdLoginPassword_PasswordChanged(object sender, RoutedEventArgs e)
        {
            CheckLoginIsAllowed();
        }

        private void txtLoginUsername_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            CheckLoginIsAllowed();
        }

        private void SwitchToRegistrationScreen()
        {
            ClearLoginScreen();
            grdLogin.Visibility = Visibility.Collapsed;
            grdRegistration.Visibility = Visibility.Visible;
            Height = 385;
            Width = 300;
        }

        private void ClearLoginScreen()
        {
            txtLoginUsername.Text = "";
            pwdLoginPassword.Password = "";
            btnLogin.IsEnabled = false;
        }

        private void CheckLoginIsAllowed()
        {
            if (txtLoginUsername.Text.Equals("") || pwdLoginPassword.Password.Equals(""))
                btnLogin.IsEnabled = false;
            else
                btnLogin.IsEnabled = true;
        }

        #endregion

        #region registration screen

        private bool passwordsSame;
        private bool usernameAvailable;

        private void btnRegister_Click(object sender, RoutedEventArgs e)
        {
            var registration = new ScrumManagementTool.login.Registration();
            registration.AddUser(txtRegUsername.Text, pwdRegPassword.Password, txtContactNo.Text, txtName.Text, txtEmail.Text);

            SwitchToLoginScreen();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            SwitchToLoginScreen();
        }

        private void txtRegUsername_LostFocus(object sender, RoutedEventArgs e)
        {
            var registration = new ScrumManagementTool.login.Registration();
            bool isAvailable = registration.CheckUsernameAvailability(txtRegUsername.Text);

            if (isAvailable)
            {
                usernameAvailable = true;
                CheckRegistrationIsAllowed();
            }
            else
            {
                usernameAvailable = false;
                btnRegister.IsEnabled = false;
            }
        }

        private void pwdRegPassword_PasswordChanged(object sender, RoutedEventArgs e)
        {
            if (pwdRegPassword.Password.Equals(pwdConfirmPassword.Password))
            {
                passwordsSame = true;
                CheckRegistrationIsAllowed();
            }
            else
            {
                passwordsSame = false;
                btnRegister.IsEnabled = false;
            }
        }

        private void pwdConfirmPassword_PasswordChanged(object sender, RoutedEventArgs e)
        {
            if (pwdRegPassword.Password.Equals(pwdConfirmPassword.Password))
            {
                passwordsSame = true;
                CheckRegistrationIsAllowed();
            }
            else
            {
                passwordsSame = false;
                btnRegister.IsEnabled = false;
            }
        }

        private void txtName_TextChanged(object sender, RoutedEventArgs e)
        {
            CheckRegistrationIsAllowed();
        }

        private void txtContactNo_TextChanged(object sender, RoutedEventArgs e)
        {
            CheckRegistrationIsAllowed();
        }

        private void txtEmail_TextChanged(object sender, RoutedEventArgs e)
        {
            CheckRegistrationIsAllowed();
        }

        private void SwitchToLoginScreen()
        {
            ClearRegistrationScreen();
            grdRegistration.Visibility = Visibility.Collapsed;
            grdLogin.Visibility = Visibility.Visible;
            Height = 135;
            Width = 300;
        }

        private void ClearRegistrationScreen()
        {
            txtRegUsername.Text = "";
            pwdRegPassword.Password = "";
            pwdConfirmPassword.Password = "";
            txtName.Text = "";
            txtContactNo.Text = "";
            txtEmail.Text = "";
            chkDeveloper.IsChecked = false;
            chkProdManager.IsChecked = false;
            chkProdOwner.IsChecked = false;
            chkScrumMaster.IsChecked = false;
            btnRegister.IsEnabled = false;
        }

        private void CheckRegistrationIsAllowed()
        {
            if (txtRegUsername.Text.Equals("") ||
               pwdRegPassword.Password.Equals("") ||
               pwdConfirmPassword.Password.Equals("") ||
               txtName.Text.Equals("") ||
               txtContactNo.Text.Equals("") ||
               txtEmail.Text.Equals("") ||
               !usernameAvailable ||
               !passwordsSame)
                btnRegister.IsEnabled = false;
            else
                btnRegister.IsEnabled = true;
        }

        #endregion

        private Window GetTopParent()
        {
            DependencyObject dpParent = this.Parent;
            do
            {
                dpParent = LogicalTreeHelper.GetParent(dpParent);
            } while (dpParent.GetType().BaseType != typeof (Window));
            return dpParent as Window;
        }
    }
}